package com.henu.engineer.service.student

import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.henu.engineer.dao.entity.student.StudentComprehensiveScore
import com.henu.engineer.dao.entity.student.StudentCourseMethod
import com.henu.engineer.dao.entity.student.StudentScore
import com.henu.engineer.dao.mapper.student.StudentComprehensiveScoreXmlMapper
import com.henu.engineer.dao.mapper.student.StudentCourseMethodXmlMapper
import com.henu.engineer.dao.mapper.student.StudentScoreXmlMapper
import com.henu.engineer.dao.mapper.student.StudentXmlMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import javax.validation.constraints.NotBlank

/**
 * 包括学生的试卷成绩，各课程的考核方式成绩，和综合成绩
 * @author noexcept
 * @date 2021/8/8 23:12
 */
@Service
class StudentScoreService {

    @Autowired
    private lateinit var mapper: StudentScoreXmlMapper

    @Autowired
    lateinit var studentCourseMethodXmlMapper: StudentCourseMethodXmlMapper

    @Autowired
    lateinit var studentComprehensiveScoreXmlMapper: StudentComprehensiveScoreXmlMapper

    @Autowired
    lateinit var studentXmlMapper: StudentXmlMapper

    /**
     * 返回学生各课程的试卷成绩
     */
    fun getStudentAllTestPaperScores(studentId: String): JSONObject {
        val data = JSONObject()
        data["studentId"] = studentId
        data["studentName"] = ""
        data["msg"] = "QUERY_SUCCESS"
        val dataArray = JSONArray()
        data["data"] = dataArray

        val student = studentXmlMapper.selectStudent(studentId)
        if (student == null) {
            data["msg"] = "NO_SUCH_ID"
            return data
        }

        data["studentName"] = student.name
        mapper.selectStudentAllScores(studentId).forEach {
            val singleCourse = JSONObject()
            singleCourse["courseId"] = it.courseId
            singleCourse["courseName"] = it.courseName
            singleCourse["score"] = it.score
            singleCourse["testPaperId"] = it.testPaperId
            singleCourse["teachingClass"] = it.teachingClass
            dataArray.add(singleCourse)
        }
        data["data"] = dataArray
        data["count"] = dataArray.size
        return data
    }

    /**
     * 返回某学生所有所有课程的所有考核方式得分
     */
    fun getMethodScore(studentId: String): JSONObject {
        val data = JSONObject()
        data["studentId"] = studentId
        data["studentName"] = ""
        data["msg"] = "QUERY_SUCCESS"
        val dataArray = JSONArray()
        data["data"] = dataArray

        val student = studentXmlMapper.selectStudent(studentId)
        if (student == null) {
            data["msg"] = "NO_SUCH_ID"
            return data
        }


        data["studentName"] = student.name
        val comprehensiveScore = studentComprehensiveScoreXmlMapper.selectByStudentId(studentId).groupBy { it.courseId }
        studentCourseMethodXmlMapper.selectAllRecordsByStudentId(studentId).groupBy { it.courseId }
            .forEach { (courseId, methodList) ->
                val course = JSONObject()
                course["score"] = comprehensiveScore[courseId]?.first()?.score
                course["courseId"] = courseId
                course["courseName"] = methodList.first().courseName
                course["classNum"] = methodList.first().classNum
                course["semester"] = methodList.first().semester
                val methodScoreData = JSONArray()
                methodList.forEach {
                    val method = JSONObject()
                    method["method"] = it.method
                    method["score"] = it.score
                    method["courseGoalId"] == it.courseGoalId
                    method["length"] = methodScoreData.size
                    methodScoreData.add(method)
                }
                course["methodScores"] = methodScoreData
                dataArray.add(course)
            }
        data["length"] = dataArray.size
        return data
    }

    /**
     * 返回学生所有课程的综合成绩
     */
    fun getComprehensiveScore(studentId: String): JSONObject {
        val res = JSONObject()
        res["studentId"] = studentId
        val data = JSONArray()
        res["data"] = data
        val student = studentXmlMapper.selectStudent(studentId)
        if (student == null) {
            res["msg"] = "NO_SUCH_ID"
            res["studentName"] = ""
            return res
        }
        res["studentName"] = student.name
        studentComprehensiveScoreXmlMapper.selectByStudentId(studentId).forEach {
            val item = JSONObject()
            item["courseId"] = it.courseId
            item["courseName"] = it.courseName
            item["score"] = it.score
            data.add(item)
        }
        res["length"] = data.size
        return res
    }
}